package com.zjw.socket.demo.starter.plugin;


import com.zjw.socket.demo.starter.annotation.core.Plugin;
import com.zjw.socket.demo.starter.config.ApplicationConfig;
import com.zjw.socket.demo.starter.core.Reflector;
import com.zjw.socket.demo.starter.utils.LogUtils;

import java.sql.Ref;
import java.util.List;

public class PluginDispatcher {

  private static final String TAG = "PluginDispatcher";


  public static void dispatchPlugin() {
    String[] plugPathList = ApplicationConfig.getPlugPathList();
    for (String plugPath : plugPathList) {
      List<Class<?>> pluginList = Reflector.reflectPackageAnnotationClass(plugPath, Plugin.class);

      for (Class<?> aClass : pluginList) {
        LogUtils.debug(TAG, "start " + aClass.getName());
        Object o = Reflector.instanceEmptyConstructor(aClass);
        if (o == null) {
          throw new IllegalArgumentException("@Plugin class must have empty constructor :" + aClass.getName());
        }

        if (!(o instanceof IPlugin)) {
          throw new IllegalArgumentException("@Plugin class must impl IPlugin :" + aClass.getName());
        }

        ((IPlugin) o).onPlugin(Reflector.vertx);

        LogUtils.debug(TAG, "end " + aClass.getName());
      }
    }
  }
}
